//
//  searchCategoryViewController.m
//  qdqts
//
//  Created by centling on 14-6-21.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "searchCategoryViewController.h"
#import "QTSearchCategoryTableViewCell.h"
#import "QTProductListViewController.h"

static NSString *cellIdentifier = @"QTSearchCategoryTableViewCell";

@interface searchCategoryViewController ()

@end

@implementation searchCategoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"分类搜索";
    
//    [self.tableView registerClass:[QTSearchCategoryTableViewCell class] forCellReuseIdentifier:cellIdentifier];
    
    UINib *nib=[UINib nibWithNibName:cellIdentifier bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
    
    UIView *emptyView = [[UIView alloc] initWithFrame:self.tableView.frame];
    self.tableView.tableHeaderView = emptyView;
    self.tableView.userInteractionEnabled = NO;
    
            [[QSClient instance] searchWithCategory:@"" completionBlock:^(QTSearchCategory *SearchCategory, NSError *error) {

                _category=[[NSMutableArray alloc]initWithArray:SearchCategory.classifications];
 
                //added by kevin li to save the productinfo
                if ([_category count] == 0) {
                    NSLog(@"could not load the product");
                    
                    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
                    //label.center = emptyView.center;
                    label.center =CGPointMake(emptyView.bounds.size.width/2, emptyView.bounds.size.height/2 - 100);
                    label.textAlignment = NSTextAlignmentCenter;
                    label.text = @"分类列表加载失败";
                    [emptyView addSubview:label];
                }
                else
                {
                    NSLog(@"load product succesfully");
                    self.tableView.tableHeaderView = nil;
                    self.tableView.userInteractionEnabled = YES;
                }
                //end added
                
                [self.tableView reloadData];
            }];


    
//	_category=[[NSMutableArray alloc]initWithObjects:@"制冷电器",@"空气调节",@"清洁卫生", @"影音视听", @"厨房电器", @"通风电器", @"个人护理与保健", nil];
//	_subCategory=[[NSMutableArray alloc]initWithObjects:@"冰箱、冷柜、酒柜、冰激凌机、制冰机...",@"空调、加湿器、除湿器、空气净化器...",@"洗衣机、干衣机、脱水机、电热水器...",@"电视机、摄录机、音响、影音播放器...", @"吸排油烟机、烹调器具、烘烤器具...", @"电风扇、换气/排气扇、冷/热风机...", @"理发器、美发器、剃/脱发器、美容仪...", nil];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    return 74;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_category count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    QTSearchCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[QTSearchCategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    Classifications * tmp=[_category objectAtIndex:indexPath.row];
    cell.categoryLabel.text = tmp.productTypeName;
    NSArray * detail= [[NSArray alloc]initWithArray:tmp.classes];
    cell.productLabel.text=@"";
    NSString * addedinfo=@"";
    for (Classes * one in detail )
    {
        NSString *  oneadd=[one.productTypeName stringByAppendingString: @"、" ];
        addedinfo= [addedinfo stringByAppendingString: oneadd];
        
    }
    
    cell.productLabel.text=addedinfo;
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    //    searchSubCategoryViewController *detailViewController = [[searchSubCategoryViewController alloc] initWithNibName:@"searchSubCategoryViewController" bundle:nil];
     QTProductListViewController *productListViewController = [[QTProductListViewController alloc] init];
    // Pass the selected object to the new view controller.
    
    productListViewController.productType=((Classifications *)[_category objectAtIndex:indexPath.row]).puid;
    productListViewController.pageTitle=((Classifications *)[_category objectAtIndex:indexPath.row]).productTypeName;
    
    // Push the view controller.
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    [self.navigationItem setBackBarButtonItem:backItem];
    [self.navigationController pushViewController:productListViewController animated:YES];
}

@end
